package com.wind.sqlite.controller;

import com.wind.sqlite.bean.PasswordBean;
import com.wind.sqlite.common.utils.R;
import com.wind.sqlite.common.validator.Assert;
import com.wind.sqlite.entity.SysUserEntity;
import com.wind.sqlite.service.SysUserService;
import com.wind.sqlite.common.utils.R;
import com.wind.sqlite.entity.SysUserEntity;
import com.wind.sqlite.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("sys/user")
@RestController
public class UserController extends AbstractController{

    @Autowired
    private SysUserService sysUserService;


    @PostMapping("/save")
    public R save(@RequestBody SysUserEntity user) {

        sysUserService.saveUser(user);

        return R.ok();
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info")
    public R info() {
        return R.ok().put("user", getUser());
    }

    @PostMapping("/password")
    public R password(@RequestBody PasswordBean form) {
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");

        //sha256加密
        String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
        //sha256加密
        String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();

        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return R.error("原密码不正确");
        }

        return R.ok();
    }
}
